package jeons.common.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;



@Service
public class FileService {
	
	/**
	* @Method Name  : uploadData
	* @�ۼ���   : 2012. 1. 16.
	* @�ۼ���   : user
	* @�����̷�  :
	* @Method ���� : ���� ���ε� 
	* @param file
	* @param folder
	* @param today
	* @return
	*/
	public boolean uploadData(MultipartFile file,String folder, String today){
		OutputStream out;
		ServletRequestAttributes path;
		out = null;
		path =  (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
		try {
			out = new FileOutputStream((new StringBuffer(String.valueOf(path.getRequest().getRealPath("")))).append(File.separator).append("upload").append(File.separator).append(folder).append(File.separator).append(today).append(file.getOriginalFilename()).toString());
			  BufferedInputStream bis = new BufferedInputStream(file.getInputStream());
	            byte buffer[] = new byte[8106];
	            int read;
	            while((read = bis.read(buffer)) > 0) 
	                out.write(buffer, 0, read);
		} catch (Exception e) {
			IOUtils.closeQuietly(out);
            return false;
		}
		IOUtils.closeQuietly(out);
		return true;
	}
	
	
	 public boolean deleteFile(String fileName) {
		 OutputStream out;
		 ServletRequestAttributes path = null;
			out = null;
	        @SuppressWarnings("null")
			File file = new File((new StringBuffer(String.valueOf(path.getRequest().getRealPath("")))).append(File.separator).append("upload").append(File.separator).append("product").append(File.separator).append(fileName).toString());
	        if(file.exists()){
	            boolean deleteFlag = file.delete();
	            System.out.println("\uC0AD\uC81C\uD558\uC600\uC2B5\uB2C8\uB2E4.");
	            return true;
	        }else{
	            System.out.println("\uC2E4\uD328");
	            return false;
	        }
	    }
	
	 
}
